import Vue from 'vue'
import Vuex from 'vuex'
import request from '@/common/request.js'
Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        /**
         * 是否需要强制登录
         */
        forcedLogin: false,
        hasLogin: false,
        userInfo: {},
		baseUrl:'http://122.51.141.95:8080',
		appId:'appId',
		appKey:'appKey'
    },
    mutations: {
		isLogin(state){
			if (!state.hasLogin) {
				uni.navigateTo({
					url: '/pages/main/login',
				});
			}
		},
        async login(state, data) {
			let res = await request.get("/user/login", {
				data: data
			});
			// 手动修改配置 setConfig方法会影响全局，请谨慎使用
			request.setConfig({
				header: {
					'token': res.token,
					'content-type': 'application/json'
				}
			});
			uni.setStorage({
				key: 'token',
				data: res.token
			});
			var userInfo=res.data;
			Vue.set(userInfo,'plaintext',data.password); //正确姿势
			state.userInfo = userInfo;
			state.hasLogin = true;
			uni.setStorage({
				key: 'userInfo',
				data: userInfo
			});
			var pages = getCurrentPages();
			if(pages.length==1){
				uni.switchTab({
					url: '/pages/shop/index'
				});
			}else{
				uni.navigateBack({
					delta:1,
					animationType:'auto'
				});
			}
        },
        logout(state) {
            state.userInfo = null;
            state.hasLogin = false;
			uni.removeStorageSync('userInfo');
			uni.removeStorageSync('currentShop');
        }
    }
})

export default store
